import re

def process_law_document(file_path):
    """
    读取法律文件并进行预处理和拆分
    
    参数:
        file_path: 法律文件路径
        
    返回:
        dict: 包含拆分后的法律文件各部分内容
    """
    # 读取文件内容
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # 预处理：删除行号和空行
    content = re.sub(r'^\d+\|\s*', '', content, flags=re.MULTILINE)
    content = re.sub(r'\n\s*\n', '\n\n', content)
    
    # 解析基本信息
    title_match = re.search(r'《(.+?)》', content)
    title = title_match.group(1) if title_match else ""
    
    issuer_match = re.search(r'^(.*?中华人民共和国.*?委员会.*?)命令', content, re.MULTILINE)
    issuer = issuer_match.group(1) if issuer_match else ""
    
    doc_number_match = re.search(r'(军发〔\d+〕\d+号)', content)
    doc_number = doc_number_match.group(1) if doc_number_match else ""
    
    issue_date_match = re.search(r'二〇(\d+)年(\d+)月(\d+)日', content)
    issue_date = f"20{issue_date_match.group(1)}年{issue_date_match.group(2)}月{issue_date_match.group(3)}日" if issue_date_match else ""
    
    effective_date_match = re.search(r'自(\d+)年(\d+)月(\d+)日起施行', content)
    effective_date = f"{effective_date_match.group(1)}年{effective_date_match.group(2)}月{effective_date_match.group(3)}日" if effective_date_match else ""
    
    # 提取目录
    toc_match = re.search(r'目录\s*(.*?)第一章', content, re.DOTALL)
    toc = "目录\n" + toc_match.group(1) if toc_match else ""
    
    # 提取章节
    chapters = []
    chapter_pattern = re.compile(r'第([一二三四五六七八九十]+)章\s*([^\n]+)', re.DOTALL)
    chapter_matches = chapter_pattern.finditer(content)
    
    for match in chapter_matches:
        chapter_num = match.group(1)
        chapter_title = match.group(2)
        chapters.append(f"第{chapter_num}章 {chapter_title}")
    
    # 改进：更精确地提取法条，避免在法条内容中提到其他法条的情况
    # 1. 先将整个文本按章节分割
    chapter_splits = re.split(r'(第[一二三四五六七八九十]+章\s*[^\n]+)', content)
    
    # 2. 在每个章节内提取法条
    articles = []
    article_pattern = re.compile(r'第([一二三四五六七八九十百千]+)条\s*(.*?)(?=\s*^第[一二三四五六七八九十百千]+条|\s*^第[一二三四五六七八九十]+章|\s*^附录|$)', re.DOTALL | re.MULTILINE)
    
    for i in range(1, len(chapter_splits), 2):
        if i+1 < len(chapter_splits):
            chapter_content = chapter_splits[i] + chapter_splits[i+1]
            article_matches = article_pattern.finditer(chapter_content)
            for match in article_matches:
                article_num = match.group(1)
                article_content = match.group(2).strip()
                articles.append(f"第{article_num}条 {article_content}")
    
    # 提取附则
    appendix_match = re.search(r'(第七章附则.*?)附录', content, re.DOTALL)
    appendix = appendix_match.group(1).strip() if appendix_match else ""
    
    # 提取附录
    attachments_match = re.search(r'附录\s*(.*?)$', content, re.DOTALL)
    attachments = attachments_match.group(0) if attachments_match else ""
    
    # 组织结果
    result = {
        "title": title,
        "issuer": issuer,
        "doc_number": doc_number,
        "issue_date": issue_date,
        "effective_date": effective_date,
        "toc": toc,
        "chapters": chapters,
        "articles": articles,
        "appendix": appendix,
        "attachments": attachments
    }
    
    return result

def parse_law_file(file_path):
    """
    读取并解析法律文件，返回处理后的列表
    
    参数:
        file_path: 法律文件路径
        
    返回:
        list: 处理后的法律文件内容列表
    """
    law_dict = process_law_document(file_path)
    
    # 将结果组织为列表
    result_list = [
        law_dict["title"],
        law_dict["issuer"],
        law_dict["doc_number"],
        law_dict["issue_date"],
        law_dict["effective_date"],
        law_dict["toc"]
    ]
    
    # 添加章节和条款
    for chapter in law_dict["chapters"]:
        result_list.append(chapter)
    
    for article in law_dict["articles"]:
        result_list.append(article)
    
    # 添加附则和附录
    if law_dict["appendix"]:
        result_list.append(law_dict["appendix"])
    
    if law_dict["attachments"]:
        result_list.append(law_dict["attachments"])
    
    return result_list

# 调用示例
law_parts = parse_law_file("old_law.txt")
for i, part in enumerate(law_parts):
    print(f"部分 {i+1}:")
    # print(part[:100] + "..." if len(part) > 100 else part)
    print(part)
    print("-" * 50)
